package com.tianma315.admin.invocie.rest;


import java.util.Arrays;
import java.util.List;

import com.tianma315.admin.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.invocie.domain.LogisticsCompanyDO;
import com.tianma315.admin.invocie.service.LogisticsCompanyService;


/**
 * 
 * <pre>
 * 物流公司表
 * </pre>
 * <small> 2019-12-17 15:02:38 | Aron</small>
 */
@Controller
@RequestMapping("/invocie/logisticsCompany")
public class LogisticsCompanyController {
	@Autowired
	private LogisticsCompanyService logisticsCompanyService;
	
	@GetMapping()
	@RequiresPermissions("invocie:logisticsCompany:logisticsCompany")
	String LogisticsCompany(){
	    return "invoice/logisticsCompany/logisticsCompany";
	}

	/**
	 *
	 */
	@GetMapping( "/all")
	@ResponseBody
	@RequiresPermissions("invocie:logisticsCompany:logisticsCompany")
	public Result<List<LogisticsCompanyDO>>  getList(){
		EntityWrapper<LogisticsCompanyDO> wrapper = new EntityWrapper<>();
		wrapper.eq("state",0);
		List<LogisticsCompanyDO> logisticsCompanyDOS = logisticsCompanyService.selectList(wrapper);
		return Result.ok(logisticsCompanyDOS);
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("invocie:logisticsCompany:logisticsCompany")
	public Result<Page<LogisticsCompanyDO>> list(Integer pageNumber, Integer pageSize, LogisticsCompanyDO logisticsCompanyDTO){
		// 查询列表数据
        Page<LogisticsCompanyDO> page = new Page<>(pageNumber, pageSize);
        Wrapper<LogisticsCompanyDO> wrapper = new EntityWrapper<LogisticsCompanyDO>(logisticsCompanyDTO);
		wrapper.orderBy("id",false);
        wrapper.eq("state",0);
        page = logisticsCompanyService.selectPage(page, wrapper);
        int total = logisticsCompanyService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("invocie:logisticsCompany:add")
	String add(){
	    return "invoice/logisticsCompany/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("invocie:logisticsCompany:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LogisticsCompanyDO logisticsCompany = logisticsCompanyService.selectById(id);
		model.addAttribute("logisticsCompany", logisticsCompany);
	    return "invoice/logisticsCompany/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("invocie:logisticsCompany:add")
	public Result<String> save( LogisticsCompanyDO logisticsCompany){
		if(logisticsCompanyService.insert(logisticsCompany)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("invocie:logisticsCompany:edit")
	public Result<String>  update( LogisticsCompanyDO logisticsCompany){
		logisticsCompanyService.updateById(logisticsCompany);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("invocie:logisticsCompany:remove")
	public Result<String>  remove( Long id){
		if(logisticsCompanyService.deleteLogComById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("invocie:logisticsCompany:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		for (int i = 0; i <ids.length ; i++) {
			logisticsCompanyService.deleteLogComById(ids[i]);
		}
		return Result.ok();
	}
}
